package com.nopay.cdxphone.ui.ativity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.inputmethod.EditorInfo;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.nopay.cdxphone.base.BaseActivity;
import com.nopay.cdxphone.bean.CommonResponse;
import com.nopay.cdxphone.bean.request.UpdatePasswordRequest;
import com.nopay.cdxphone.utils.RememberUitls;

import com.nopay.cdxphone.Constants;
import com.nopay.cdxphone.R;
import com.nopay.cdxphone.databinding.ActivityUpdatePasswordBinding;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ProjectName: myVisualPhone
 * @Package: cn.cn.myvisualphone.ui.ativity
 * @ClassName: ForgetActivity
 * @Description: java类作用描述
 * @Author: anqi
 * @CreateDate: 2021/5/23 8:05
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/5/23 8:05
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class UpdatePasswordActivity extends BaseActivity<ActivityUpdatePasswordBinding> {

    @Override
    protected void init(Bundle savedInstanceState, Object... preData) {
        binding.rlRoot.setPadding(0, getStatusBarHeight(UpdatePasswordActivity.this), 0, 0);
        binding.ivBack.setOnClickListener(v -> mSwipeBackHelper.backward());

        binding.loginEtRepwd.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                if(checkInput()) doForget();
                return true;
            }
            return false;
        });

        binding.commitBtn.setOnClickListener(v -> {
            if(checkInput()) doForget();
        });
    }

    private void doForget() {
        String userPhone = binding.loginEtUser.getText().toString().trim();
        String password = binding.loginEtOldPwd.getText().toString().trim();
        String pwd = binding.loginEtPwd.getText().toString().trim();
        String repwd = binding.loginEtRepwd.getText().toString().trim();
        UpdatePasswordRequest request = new UpdatePasswordRequest();
        request.setPhone(userPhone);
        request.setPassword(password);
        request.setNewPassword(pwd);
        request.setRepNewPassword(repwd);
        //配置请求对象结束
        RequestBody body = getRequestBody(request);
        api.doUpdatePassword(body).enqueue(new Callback<CommonResponse>() {
            @Override
            public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {
                if (response.body() != null) {
                    if (Constants.RESULT_OK.equals(response.body().getCode())) {
                        ToastUtils.showShort(response.body().getMsg());
                        if (!TextUtils.isEmpty(SPUtils.getInstance().getString(Constants.SP_REMEMBER))) {
                            String[] split = RememberUitls.getRemember();
                            String userName = split[0];
                            RememberUitls.setRemember(userName, pwd);
                        }
                        mSwipeBackHelper.backward();
                    } else {
                        ToastUtils.showShort(response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<CommonResponse> call, Throwable t) {
                ToastUtils.showShort(UpdatePasswordActivity.this.getResources().getString(R.string.call_api_error));
            }
        });
    }

    private boolean checkInput() {
        String userPhone = binding.loginEtUser.getText().toString().trim();
        String pwd = binding.loginEtOldPwd.getText().toString().trim();
        String newpwd = binding.loginEtPwd.getText().toString().trim();
        String repwd = binding.loginEtRepwd.getText().toString().trim();
        if (TextUtils.isEmpty(userPhone)) {
            ToastUtils.showShort(getResources().getString(R.string.please_input_phone_number));
            return false;
        }
        if (TextUtils.isEmpty(pwd)) {
            ToastUtils.showShort(getResources().getString(R.string.please_input_phone_pwd));
            return false;
        }
        if (TextUtils.isEmpty(newpwd)) {
            ToastUtils.showShort(getResources().getString(R.string.please_input_new_phone_pwd));
            return false;
        }
        if (TextUtils.isEmpty(repwd)) {
            ToastUtils.showShort(getResources().getString(R.string.please_reinput_new_phone_pwd));
            return false;
        }
        if (newpwd.length() < 6) {
            ToastUtils.showShort(getResources().getString(R.string.pwd_less_six));
            return false;
        }
        if (!newpwd.equals(repwd)) {
            ToastUtils.showShort(getResources().getString(R.string.different_pwd));
            return false;
        }
        if (!RegexUtils.isMobileExact(userPhone)) {
            ToastUtils.showShort(getResources().getString(R.string.please_input_right_phone_number));
            return false;
        }

        return true;
    }

}
